/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;

import java.rmi.RemoteException;
import java.util.ArrayList;


/**
 * Client-side stub of IActionLocation class. It catches all the exceptions thrown 
 * by the RMI methods.
 */
public class ActionLocationStub {
	
	
	/**
	 * Actual RMI object
	 */
	private IRmiActionLocation actionLocation;

	
	/**
	 * Constructor: sets actual RMI object
	 */
	public ActionLocationStub(IRmiActionLocation actionLocation) {
		this.actionLocation = actionLocation;
	}
	
	
	/**
	 * Returns a list of all tower elements in the selection of this market.
	 */
	public ArrayList<TowerElement> getTowerElementSelection() {
		ArrayList<TowerElement> selection = new ArrayList<TowerElement>();
		try {
			for(int id : actionLocation.getTowerElementSelectionIDs()) {
				selection.add(new TowerElement(id));
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return selection;
	}
	
	
	/**
	 * Returns a list of all tower elements in the pile of this market.
	 */
	public ArrayList<TowerElement> getTowerElementPile() {
		ArrayList<TowerElement> pile = new ArrayList<TowerElement>();
		try {
			for(int id : actionLocation.getTowerElementPileIDs()) {
				pile.add(new TowerElement(id));
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return pile;
	}

	
	/**
	 * Returns array of the slots where buying agent cards can be placed. 
	 * (0 = no card, 1 = one card in the color of the color constraint, 2 = two cards)
	 */
	public int[] getBuyingAgentSlots() {
		int[] buyingAgentSlots = {};
		try {
			buyingAgentSlots = actionLocation.getBuyingAgentSlots();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return buyingAgentSlots;
	}
	
	
	/**
	 * Returns true if there is still at least one free buying agent card slot.
	 */
	public boolean hasFreeBuyingAgentSlot() {
		for(int slot : getBuyingAgentSlots()) {
			if(slot == 0) {
				return true;
			}
		}
		return false;
	}

	
	/**
	 * Returns color of color constraint.
	 */
	public BuyingAgent.Color getColorConstraint() {
		BuyingAgent.Color color = BuyingAgent.Color.NONE;
		try {
			color = BuyingAgent.intToEnum(actionLocation.getColorConstraint());	
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return color;
	}

	
	/**
	 * Carry out the action of the action location. (See action location class for doAction() version.)
	 * If only one buying agent card is used: buyingAgentID2 = -1 
	 */
	public boolean doAction(int playerIndex, int buyingAgentID1, int buyingAgentID2) {
		boolean success = false;
		try {
			success = actionLocation.doAction(playerIndex, buyingAgentID1, buyingAgentID2);	
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return success;
	}
	
	public boolean doAction(int playerIndex, int buyingAgentID1, int buyingAgentID2, int parameter1) {
		boolean success = false;
		try {
			success = actionLocation.doAction(playerIndex, buyingAgentID1, buyingAgentID2, parameter1);	
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return success;
	}

	public boolean doAction(int playerIndex, int buyingAgentID1, int buyingAgentID2, int parameter1, int parameter2) {
		boolean success = false;
		try {
			success = actionLocation.doAction(playerIndex, buyingAgentID1, buyingAgentID2, parameter1, parameter2);	
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return success;
	}

	public boolean doAction(int playerIndex, int buyingAgentID1, int buyingAgentID2, int parameter1, String parameter2) {
		boolean success = false;
		try {
			success = actionLocation.doAction(playerIndex, buyingAgentID1, buyingAgentID2, parameter1, parameter2);	
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return success;
	}

}
